package sk.stuba.fiit.repka.dp.owl.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import sk.stuba.fiit.repka.dp.gui.SelectFromCheckBoxesSP;
import sk.stuba.fiit.repka.dp.owl.MyOWLClass;
import sk.stuba.fiit.repka.dp.owl.OWLComponent;

public class SelectRangeDialog extends JDialog implements ActionListener{
	
	private List<MyOWLClass> classes; //input to dialog
	
	private List<MyOWLClass> getClassesList() {
		if(this.classes == null) {
			this.classes = new ArrayList<MyOWLClass>();
		}
		return this.classes;
	}
	
	private List<String> outputRanges; //output from dialog ... all ranges which will be mapped to property
	
	public List<String> getOutputRanges() {
		if(this.outputRanges == null) {
			this.outputRanges = new ArrayList<String>();
		}
		
		return this.outputRanges;
	}
	
	private final JPanel contentPanel = new JPanel();
	private SelectFromCheckBoxesSP selectFromSP;
	private JComboBox comboBox;
	
	private final String[] comboDataTypes = {"Literal", "NCName", "NMTOKEN", "Name", "PlainLiteral", "XMLLiteral", "anyURI",
													 "base64Binary", "boolean", "byte", "dateTime", "dateTimeStamp", "decimal", "double",
													 "float", "hexBinary", "int", "integer", "language", "long", "negativeInteger", "nonNegativeInteger",
													 "nonPositiveInteger", "normalizedString", "positiveInteger", "rational", "real", "short", "string", 
													 "token", "unsignedByte", "unsignedInt", "unsignedLong", "unsignedShort" 
													};
	
	private boolean selectFromSPVisibility = false;
	private boolean insertOutpuPanelVisibility = false;
	
	/**
	 * Create the dialog.
	 */
	public SelectRangeDialog(List<MyOWLClass> classes, boolean selectFromSPVisibility, boolean insertOutpuPanelVisibility) {
		this.selectFromSPVisibility = selectFromSPVisibility;
		this.insertOutpuPanelVisibility = insertOutpuPanelVisibility;
		this.classes = new ArrayList<MyOWLClass>(classes); //create list ... changes in new list should not have affect in old list!!!
		this.setModal(true);
		setTitle("Define Range");
		setBounds(100, 100, 635, 319);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new GridLayout(1, 0, 0, 0));
		{
			selectFromSP = new SelectFromCheckBoxesSP(this.getClassesList());
			selectFromSP.setBorder(new TitledBorder(null, "Select", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			selectFromSP.setVisible(selectFromSPVisibility);
			
			JPanel insertOutpuPanel = new JPanel();
			insertOutpuPanel.setBorder(new TitledBorder(null, "Insert ", TitledBorder.LEADING, TitledBorder.TOP, null, null));
			insertOutpuPanel.setVisible(insertOutpuPanelVisibility);
			JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, selectFromSP, insertOutpuPanel);
			splitPane.setOneTouchExpandable(true);
			splitPane.setDividerLocation(300);
			contentPanel.add(splitPane);
				
			splitPane.setRightComponent(insertOutpuPanel);
			{
				JLabel lblInsertRange = new JLabel("Insert Range");
				insertOutpuPanel.add(lblInsertRange);
			}
			{
				comboBox = new JComboBox(comboDataTypes);
				insertOutpuPanel.add(comboBox);
			}
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(this);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(this);
				buttonPane.add(cancelButton);
			}
		}
		this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		this.setVisible(true);
	}
	
	//create output ... ranges which will be mapped to property
	private void createOutput(List<? extends MyOWLClass> selectedClasses, String textfieldInput) {
		Iterator<? extends OWLComponent> iter = selectedClasses.iterator();
		while(iter.hasNext()) {
			this.getOutputRanges().add(iter.next().getName());
		}
		if(textfieldInput != null && !"".equals(textfieldInput) && this.insertOutpuPanelVisibility) {
			this.getOutputRanges().add(textfieldInput);
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if("OK".equals(e.getActionCommand())) {
			List<MyOWLClass> selectedClasses = new ArrayList<MyOWLClass>(selectFromSP.getSelectedOWLClassesFromCheckBoxes()); //get all selected classes
			this.createOutput(selectedClasses, comboBox.getSelectedItem().toString());
			this.dispose();
		} else if("Cancel".equals(e.getActionCommand())) {
			this.dispose();
		}
		
	}

}
